# Maintainer: David Macek <david.macek.0@gmail.com>

# todo check if unix directory is interesting
# todo install docs

_realname=putty
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.83
pkgrel=1
pkgdesc="A free telnet/SSH client (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://www.chiark.greenend.org.uk/~sgtatham/putty/"
msys2_repository_url="https://git.tartarus.org/?p=simon/putty.git"
msys2_references=(
  "cpe: cpe:/a:putty:putty"
  "cpe: cpe:/a:simon_tatham:putty"
)
license=('spdx:MIT')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-cc")
optdepends=("${MINGW_PACKAGE_PREFIX}-putty-ssh: ssh wrapper for plink")
replaces=("${MINGW_PACKAGE_PREFIX}-${_realname}-git"
          "${MINGW_PACKAGE_PREFIX}-${_realname}-svn")
source=("https://the.earth.li/~sgtatham/putty/latest/${_realname}-${pkgver}.tar.gz"
        "putty-${pkgver}.tar.gz.sig::https://the.earth.li/~sgtatham/putty/${pkgver}/putty-${pkgver}.tar.gz.gpg")
sha256sums=('718777c13d63d0dff91fe03162bc2a05b4dfc8b0827634cd60b51cefdff631c6'
            'SKIP')
validpgpkeys=('F412BA3AA30FDC0E77B4E3871993D21BCAD1AA77') # PuTTY Releases <putty@projects.tartarus.org>

prepare() {
  mkdir -p "${srcdir}"/${_realname}-build-${MSYSTEM}
}

build() {
  cd "${srcdir}"/${_realname}-build-${MSYSTEM}

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -GNinja \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    ${_extra_config[@]} \
    ../${_realname}-${pkgver}
  ${MINGW_PREFIX}/bin/cmake.exe --build .
}

package() {
  cd "${srcdir}"/${_realname}-${pkgver}
  install -D -m644 LICENCE "${pkgdir}${MINGW_PREFIX}"/share/licenses/${_realname}/LICENSE

  cd "${srcdir}"/${_realname}-build-${MSYSTEM}
  for f in putty puttygen pageant plink pscp psftp psocks puttytel
  do
    install -D -m755 ${f}.exe "${pkgdir}"/${MINGW_PREFIX}/bin/${f}.exe
  done
}
